package com.neu.yth.nepBackend.grid.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.yth.nepBackend.entity.Grid;
import com.neu.yth.nepBackend.grid.service.IGridService;
import com.neu.yth.nepBackend.utils.BufferedFileIo;
import com.neu.yth.nepBackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 区域网格信息 前端控制器
 * </p>
 *
 * @author YTH
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/grid")
public class GridController {
    @Autowired
    IGridService gridService;

    @GetMapping("select/all/tree")
    public String selectAllGridTree() throws IOException {
        String json = null;
        File file = new File("C:\\Users\\YTH\\Desktop\\grid.json");
        if (file.exists()) {
            json = BufferedFileIo.readStringFromFile();
        } else {
            Result<Grid> result = gridService.selectAllGridTree();
            if (result.getCode() == 200) {
                ObjectMapper mapper = new ObjectMapper();
                json = mapper.writeValueAsString(result);
                BufferedFileIo.writeStringToFile(json);
            }
        }
        return json;
    }
}
